package com.net.tkc.pro.controller;


import com.net.tkc.pro.entity.Post;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.entity.Volunteersactivity;
import com.net.tkc.pro.impl.VolunteersactivityImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/volController/")
public class VolunteersactivityController {
    ResultEntity resultEntity = null;
    @Resource
    VolunteersactivityImpl volunteersactivity;

    @GetMapping("List2")
    private String geiList2(
            @RequestParam("thisTitle") String thisTitle
    ){
        resultEntity = new ResultEntity();
        try{
            boolean flag=false;
            if(!thisTitle.equals("") || thisTitle !=null){
                flag=true;
            }
            if(!thisTitle.equals("") || thisTitle !=null){
                thisTitle="%"+thisTitle+"%";
            }
            List<Volunteersactivity> list =volunteersactivity.getList2(thisTitle);
            if(flag){
                resultEntity.setCount(list.size());
            }else{
                resultEntity.setCount(volunteersactivity.getList());
            }
            resultEntity.setCode("0");
            resultEntity.setData(list);
            resultEntity.setMsg("获取成功！");
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 无条件查询Class里面的数据
     * @return
     */

    @GetMapping("List")
    private String geiList(
    ){
        resultEntity = new ResultEntity();
        try{
            List<Volunteersactivity> list=volunteersactivity.getList();
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    @GetMapping("activity")
    private String getActivity(
            @RequestParam("userid") int userid
    ){
        resultEntity = new ResultEntity();
        try{
            List<Volunteersactivity> list=volunteersactivity.getActivity(userid);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
